/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.gui.pages;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import fsahoraires.cours.Cours;
import fsahoraires.gui.MainWindow;
import fsahoraires.programmes.Semestre;

public class SemestreCoursPage extends ListPage {

	private final MainWindow main;
	private final Semestre semestre;

	private Text code;

	/**
	 * 
	 * @param main
	 * @param semestre le semestre ou <code>null</code> pour voir la cache
	 *            entière
	 */
	public SemestreCoursPage(MainWindow main, Semestre semestre) {
		super("Cours", "cours_edit.png");
		this.main = main;
		this.semestre = semestre;
	}

	@Override
	protected Control createEntry(Composite parent) {
		final Composite comp = new Composite(parent, SWT.NONE);
		final Label label = new Label(comp, SWT.NONE);
		label.setText("Code : ");
		label.pack();
		code = new Text(comp, SWT.BORDER);
		code.pack();
		// layout
		comp.setLayout(new FormLayout());
		FormData fd = new FormData();
		fd.top = new FormAttachment(50, -label.getSize().y / 2);
		fd.left = new FormAttachment(0);
		label.setLayoutData(fd);
		fd = new FormData();
		fd.top = new FormAttachment(50, -code.getSize().y / 2);
		fd.left = new FormAttachment(label);
		fd.right = new FormAttachment(100);
		code.setLayoutData(fd);
		// listeners
		code.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if (e.character == '\r') {
					e.doit = false;
					add();
					code.setFocus();
					code.selectAll();
				}
			}
		});
		return comp;
	}

	@Override
	protected void initTable() {
		initColumns(new String[] { "Code", "Intitulé", "Crédits", "Heures" },
				new int[] { SWT.LEFT, SWT.LEFT, SWT.CENTER, SWT.CENTER });
	}

	protected void packColumns(int width) {
		for (int i = 0; i < table.getColumnCount(); i++) {
			if (i != 1) {
				table.getColumn(i).pack();
				width -= table.getColumn(i).getWidth();
			}
		}
		table.getColumn(1).setWidth(width);
	}

	@Override
	public void update() {
		table.removeAll();

		for (Cours cours : (semestre != null ? semestre : main.getProgramme()
				.getManager())) {
			addCours(cours);
		}
		packColumns();
	}

	private void addCours(Cours cours) {
		TableItem ti = new TableItem(table, SWT.NONE);
		ti.setText(0, cours.getCode());
		ti.setText(1, cours.getTitre());
		ti.setText(2, cours.getCredits() + "");
		ti.setText(3, cours.getHeures() + "");
		ti.setData(cours);
	}

	@Override
	protected void add() {
		Cours cours = main.getProgramme().getManager().getCours(code.getText());
		if (semestre != null)
			semestre.add(cours);
	}

	@Override
	protected void remove() {
		for (TableItem ti : table.getSelection()) {
			Cours cours = (Cours) ti.getData();
			if (semestre == null)
				cours.dispose();
			else
				semestre.remove(cours);
		}
		packColumns();
	}

}
